(function(app){
	app
		.controller("content_02_1_listCtrl",["$scope",'$rootScope',"myFactory","$ionicHistory",'$http','$stateParams','$state',function($scope,$rootScope,myFactory,$ionicHistory,$http,$stateParams,$state){
			
			//接收参数
			var itemId=$stateParams.itemId;
			var item=$stateParams.item;
			var chdrcoy=$stateParams.chdrcoy;
			var chdrnum01 = $stateParams.chdrnum01
			var initFlag = false;
			var token=sessionStorage.getItem("token");
			//绑定参数
			$scope.chdrnum01=$stateParams.chdrnum01;
			
			$scope.data = {
				allCheck: true, //全选标志
				list: [//采用$scope动态绑定填装
				]
			}

/*******************************************************初始化函数begin************************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-strSelectedBdh-token-timestamp";
				var signStr="itemId="+itemId+"&strSelectedBdh="+""+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化访问后台，查询出属其他保单号
				$http({
					method: 'GET',
					url: app.domain + 'input/saveContentItem02_1.action',
					//params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy}
					params: {'itemId': itemId,'itemStr':item,"strSelectedBdh":"",'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						//alert(response.data.data.itemData.chdrlist[0].rnName);
						$scope.data.list=response.data.data.itemData.chdrlist
						//不管有几个，初始化全部选中
						for (var i = $scope.data.list.length - 1; i >= 0; i--) {
							$scope.data.list[i].checkState = true;
						};
						initFlag = true;
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询信息失败!","info");
				});
			}
/*******************************************************初始化函数begin************************************************************/
			//全选控制
			$scope.changeAll = function(){
				console.log($scope.data.allCheck);
				if ($scope.data.allCheck == true) {
					for (var i = $scope.data.list.length - 1; i >= 0; i--) {
						if ($scope.data.list[i].checkState != true) {
							$scope.data.list[i].checkState = true;
						};
					};
				} else{
					for (var i = $scope.data.list.length - 1; i >= 0; i--) {
						if ($scope.data.list[i].checkState == true) {
							$scope.data.list[i].checkState = false;
						};
					};
				};
			}

			//返回函数
			$scope.goBack = function () {
				/*
				 * S2018062604412网联政策630实施相关系统功能改造需求（移动保全2.0增加签约功能）
				 * 新增签约接口的逻辑 add by csf 20180926 start
				 */
				if( $rootScope['flag']  =='1' ){ 
					$rootScope['flag'] ='';
					$ionicHistory.goBack(-2);
				} else{
					myFactory.goBack();
				}
				/**
				 * S2018062604412网联政策630实施相关系统功能改造需求（移动保全2.0增加签约功能）
				 * 新增签约接口的逻辑 add by csf 20180926 end
				 */
				//myFactory.goBack();
			}
			
			//点击下一步
			$scope.nextStep = function () {
				//判断是否初始化完成了，如果失败，不予进入项目
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				//定义用于拼装保单号
				var strSelectedBdh="";
				var flagStatus=false;
				
				//拼装保单号
				for (var i = $scope.data.list.length - 1; i >= 0; i--) {
					if ($scope.data.list[i].checkState == true) {
						if(flagStatus==false){
							flagStatus=true;
							strSelectedBdh += $scope.data.list[i].chdrnum;
						 }else{
							 strSelectedBdh += "," + $scope.data.list[i].chdrnum;
						 }
						//strSelectedBdh=$scope.data.list[i].chdrnum01;
					}
				}
				flagStatus=false;
				console.log("strSelectedBdh:"+strSelectedBdh);

				item["sourceFlag"] = "2";//改变状态

//              很明显的重复内容，这一步放到check_main初始化做即可 ZZQ 2017-8-30
//				$http({
//					method: 'GET',
//					url: app.domain + 'input/saveContentItem02_1.action',
//					params: {'itemId': itemId,'itemStr':item,"strSelectedBdh":strSelectedBdh}
//				}).then(function successCallback(response) {
//					if (response.data.success) {
//						//如果成功，跳转check_main
//						$state.go('content_check_main',{
//							itemId:itemId,
//							item:item,
//							chdrnum01:chdrnum01,
//							chdrcoy:chdrcoy
//						});
//					}
//				}, function errorCallback(response) {
//					$.alert({
//						 title: '',
//						 text: '提交数据时出错!',
//						 onOK: function () {
//						 //点击确认
//						 }
//					});
//				});
				$ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						strSelectedBdh:strSelectedBdh //额外穿过去同时要变更的保单项
					});
				})
				
				
			}

		}])
})(app)